package com.ruitu.ssmic;

import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx;
import com.ruitu.arad.base.BaseActivity;
import com.ruitu.arad.support.view.NoScrollViewPager;
import com.ruitu.arad.util.ScreenUtils;
import com.ruitu.arad.util.SizeUtils;
import com.ruitu.arad.util.ToastUtils;
import com.ruitu.arad.view.MoveView;
import com.ruitu.router_module.Router;
import com.ruitu.ssmic.mvp.model.MainModel;
import com.ruitu.ssmic.mvp.presenter.MainPresenter;
import com.ruitu.ssmic.ui.MainHomeFragment;
import com.ruitu.ssmic.ui.PropertyFragment;
import com.ruitu.ssmic.ui.Test1Activity;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

public class MainActivity extends BaseActivity<MainPresenter, MainModel> {
    private int SCREEN_WIDTH, SCREEN_HEIGHT, size0, size1;
    private BottomNavigationViewEx bnve;
    private NoScrollViewPager vp;
    private LinearLayout ll_quick_open;//快捷开门
    private MoveView mv;//mv
    private View float_btn;//

    private VpAdapter adapter;

    private int n = 0;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_main;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        bnve = findViewById(R.id.bnve);
        vp = findViewById(R.id.vp);
        ll_quick_open = findViewById(R.id.ll_quick_open);
        mv = findViewById(R.id.mv);
        float_btn = View.inflate(this, R.layout.float_btn_layout, null);

        setListeners();

        vp.setOffscreenPageLimit(9);
        vp.setScanScroll(false);

        bnve.enableAnimation(false);
        bnve.enableShiftingMode(false);
        bnve.enableItemShiftingMode(false);

        bnve.setIconsMarginTop(SizeUtils.dp2px(10));
        bnve.setIconSize(23, 23);
        bnve.setTextSize(10);
        bnve.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                return true;
            }
        });

        adapter = new VpAdapter(getSupportFragmentManager());
        vp.setAdapter(adapter);
        bnve.setupWithViewPager(vp);

        SCREEN_WIDTH = ScreenUtils.getScreenWidth();
        SCREEN_HEIGHT = ScreenUtils.getScreenHeight();
        size0 = SizeUtils.getMeasuredWidth(ll_quick_open) / 2;
        size1 = SizeUtils.getMeasuredHeight(ll_quick_open) / 2;
//        setQuickIOpenListner();
//        mv.setVisibility(View.GONE);
        ll_quick_open.setVisibility(View.GONE);
//        addFloatBtnToWindow();
    }

    private void setListeners() {
        mv.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
//        if (v == tv_login) {
//            try {
//                Router.startActivity(this,"com.ruitu.ssmic.LoginActivity");
//            } catch (ClassNotFoundException e) {
//                e.printStackTrace();
//            }
//        }
        if (v == mv) {
            ToastUtils.showShortSafe("开门");
        }
    }

    @Override
    public void onReqComplete(int code, boolean isOk, Object data) {
        super.onReqComplete(code, isOk, data);
        if (code == 1) {
            showReqResultDialog("温馨提示", (String) data, "再试一次", "直接退出");
        }
    }

    @Override
    protected void onAlertHandle() {
        super.onAlertHandle();
        if (n == 0) {
            showReqResultDialog("温馨提示", "再点一次试试嘛~", "再试一次", "直接退出");
        } else {
            startActivity(Test1Activity.class);
        }
        n++;
    }

    @Override
    protected void onAlertCancel() {
        super.onAlertCancel();
    }

    private class VpAdapter extends FragmentPagerAdapter {

        public VpAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            if (position == 0) {//首页
                return MainHomeFragment.newInstance();
            } else if (position == 1) {//资产
                return PropertyFragment.newInstance();
            } else if (position == 2) {//生活 LifeFragment
                Fragment fragment = Router.getFragment("com.ruitu.ssmic.ui.fragment.LifeFragment");
                if (null != fragment) {
                    return fragment;
                } else {
                    ToastUtils.showShort("加载生活模块失败");
                }
            } else if (position == 3) {//我的 MineFragment
                Fragment fragment = Router.getFragment("com.ruitu.ssmic.ui.fragment.MineFragment");
                if (null != fragment) {
                    return fragment;
                } else {
                    ToastUtils.showShort("加载我的模块失败");
                }
            }

            return MainHomeFragment.newInstance();
        }

        @Override
        public int getCount() {
            return 5;
        }
    }

    private void addFloatBtnToWindow() {
//        1.获取WindowManager服务：
        WindowManager wmManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
//        2.设置WindowManager.LayoutParams参数
        WindowManager.LayoutParams wmParams = new WindowManager.LayoutParams();
//        wmParams.type = WindowManager.LayoutParams.TYPE_PHONE; // 设置window type
        wmParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY; // 设置window type
        wmParams.format = PixelFormat.RGBA_8888; // 设置图片格式，效果为背景透明
        wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
        wmParams.gravity = Gravity.RIGHT | Gravity.CENTER_VERTICAL; // 调整悬浮窗口至右侧中间
        wmParams.x = 0;// 以屏幕左上角为原点，设置x、y初始值
        wmParams.y = 0;
        wmParams.width = WindowManager.LayoutParams.MATCH_PARENT;// 设置悬浮窗口长宽数据
        wmParams.height = WindowManager.LayoutParams.MATCH_PARENT;
//        3.添加view到屏幕
        wmManager.addView(float_btn, wmParams);
    }

    private int lastX, lastY;    //保存手指点下的点的坐标

    private void setQuickIOpenListner() {
        ll_quick_open.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent event) {
                if (event.getRawX() < 30 || event.getRawX() > SCREEN_WIDTH - SizeUtils.dp2px(30)
                        || event.getRawY() < SizeUtils.dp2px(70)
                        || event.getRawY() > SCREEN_HEIGHT - SizeUtils.dp2px(100)) {
                    return true;
                }
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        //将点下的点的坐标保存
                        lastX = (int) event.getRawX();
                        lastY = (int) event.getRawY();
                    case MotionEvent.ACTION_MOVE:
                        //计算出需要移动的距离
                        int dx = (int) event.getRawX() - lastX;
                        int dy = (int) event.getRawY() - lastY;
                        //将移动距离加上，现在本身距离边框的位置
                        int left = view.getLeft() + dx;
                        int top = view.getTop() + dy;
                        //获取到layoutParams然后改变属性，在设置回去
                        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
//                        layoutParams.height = IMAGE_SIZE;
//                        layoutParams.width = IMAGE_SIZE;
                        layoutParams.leftMargin = left;
                        layoutParams.topMargin = top;
                        view.setLayoutParams(layoutParams);
                        //记录最后一次移动的位置
                        lastX = (int) event.getRawX();
                        lastY = (int) event.getRawY();
                        break;
                    default:
                        break;
                }
                return true;
            }
        });
    }
}
